package com.leandom.studydemo.contentresolver;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


public class TestContentProvider extends ContentProvider {

    private static final String TAG = "DEMO";
    public static final String CONTENT_AUTHORITY = "com.leandom.studydemo.testProvider";
    public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
    public static final String PATH_TEST = "test";
    static final int TEST = 100;

    static UriMatcher buildUriMatcher() {
        final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI(CONTENT_AUTHORITY, PATH_TEST, TEST);
        // 通配符*只能匹配一级，eg：root/* 能匹配root/123 不能匹配 root/123/1
        matcher.addURI(CONTENT_AUTHORITY, "root/*", 101);
        return matcher;
    }

    static {
        Uri test1 = new Uri.Builder()
                .scheme("content")
                .authority(TestContentProvider.CONTENT_AUTHORITY)
                .path("root/123") // 101
                // .path("root/123/1") // -1
                .build();

        int match = buildUriMatcher().match(test1);
        Log.d(TAG, test1 + " match: " + match);
    }

    @Override
    public boolean onCreate() {
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
                        @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        switch (buildUriMatcher().match(uri)) {
            case TEST: {
                String[] columnNames = new String[]{"id", "name"};
                List<List<Object>> list = new ArrayList<>();
                list.add(new ArrayList<Object>());
                list.add(new ArrayList<Object>());

                list.get(0).add(1);
                list.get(0).add("张三");

                list.get(1).add(1);
                list.get(1).add("李四");
                MyCursor cursor = new MyCursor(list, columnNames);
                Log.d(TAG, "setNotificationUri: " + uri);
                cursor.setNotificationUri(getContext().getContentResolver(), uri);
                return cursor;
            }
            default:
                throw new android.database.SQLException("Unknown uri: " + uri);
        }
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        switch (buildUriMatcher().match(uri)) {
            case TEST: {
                // xxxx 插入数据库
                Log.d(TAG, "notifyChange: " + uri);
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            }
        }
        return uri;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
                      @Nullable String[] selectionArgs) {
        return 0;
    }
}
